package org.shock.ss.sms.action.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import net.sf.json.JSONArray;
import org.shock.ss.sms.commons.annotation.RightAnno;
import org.shock.ss.sms.commons.db.DbTools;
import org.shock.ss.sms.bean.Page;
import org.shock.ss.sms.bean.Pager;
import org.shock.ss.sms.commons.utils.ParamsUtil;
import org.shock.ss.sms.commons.utils.ReflexUtils;
import org.shock.ss.sms.commons.utils.Util;

import org.shock.ss.sms.bean.Tree;
import org.shock.ss.sms.bean.system.Module;
import org.shock.ss.sms.bean.system.Right;

import org.shock.ss.sms.commons.dao.DaoFactory;
import org.shock.ss.sms.dao.system.impl.RightDaoImpl;

import org.shock.ss.sms.service.system.RightService;
import org.shock.ss.sms.service.system.impl.RightServiceImpl;
import org.shock.ss.sms.action.BaseAction;

/**
 * 权限管理action
 * 
 * @author jile
 * 
 */
public class RightAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	/** 获得service */
	private RightService rs = new RightServiceImpl((RightDaoImpl) DaoFactory
			.getDao("RightDao"));
	private Right right = new Right();
	private List list = new ArrayList();
	private Map map = new HashMap();
	private Long roleid;
	private Long node;

	public String execute() throws Exception {
		return SUCCESS;
	}

	@RightAnno(rightValue = "viewRight", rightNote = "查看权限")
	public String viewRight() throws Exception {
		String sql = "select r.*,m.name as modname from "+Right.getTable()+" r left join "+Module.getTable()+" m on r.modid=m.id where r.id="
				+ right.getId();
		//System.out.println(sql);
		map = rs.view(sql);
		//System.out.println(map);
		return SUCCESS;
	}
	/**
	 * 增加或更新权限 根据bean 的id进行判断，如果id为null则为增加，否则为更新。
	 * 
	 * @return
	 * @throws Exception
	 */
	public String saveRight() throws Exception {
		BeanUtils.populate(right, map);
		//System.out.println(ReflexUtils.rwritertoString(right));
		if (right.getId() == null || right.getId() == 0) {
			addRight();
		} else {
			updateRight();
		}
		return SUCCESS;
	}

	@RightAnno(rightValue = "addRight", rightNote = "权限添加")
	public String addRight() throws Exception {
		log.debug(ReflexUtils.rwritertoString(right));// 调试bean信息
		if (action.equals("before")) {// 显示添加页面
			return "before";
		}
		rs.add(right);
		message = "添加成功！";
		jmpUrl = "listRight.do";
		return SUCCESS;
	}

	@RightAnno(rightValue = "updateRight", rightNote = "修改权限")
	public String updateRight() throws Exception {
		if (action.equals("before")) {// 显示添加页面
			String sql = "select r.*,m.name as modname from "+Right.getTable()+" r left join "+Module.getTable()+" m on r.modid=m.id where r.id="
					+ right.getId();
			// BeanUtils.populate(right, rs.view(sql));
			map = rs.view(sql);
			return "before";
		}
		rs.update(right);
		message = "更新成功！";
		jmpUrl = "listRight.do";
		return SUCCESS;
	}

	@RightAnno(rightValue = "public", rightNote = "权限列表")
	public String listRight() throws Exception {
		/**  查询条件*/
		if (null!=right) {
			if (!Util.isEmpty(right.getName())) {
			sqladd.append("and name like '%"+right.getName().trim()+"%'");	
			}
			if (!Util.isEmpty(right.getCode())) {
				sqladd.append(" and code like '%"+right.getCode().trim()+"%'");
			}
		}
		Pager pager = new Pager();
		String sul = ParamsUtil.transParams(getRequest().getParameterMap());
		String sqlcount = "select count(1) from "+Right.getTable()+" where 1=1 " + sqladd;
		String sql = "select * from "+Right.getTable()+" where 1=1 " + sqladd;
		//System.out.println(sql);
		pager = rs.list("asc", "modid", pageno, rownum, sqlcount, sql);
		list = pager.getList();
		Page page = new Page();
		page.setPageString(pager.getTotalCount(), rownum, pageno, pagerow, sul);
		pageStr = page.getPageString();
		return SUCCESS;
	}

	@RightAnno(rightValue = "delRight", rightNote = "删除权限")
	public String delRight() throws Exception {
		String sql = "delete from "+Right.getTable()+" where id =" + right.getId();
		rs.del(sql);
		message = "删除成功！";
		jmpUrl = "listRight.do";
		return SUCCESS;
	}

	public String listAllRight() throws Exception {
		if (roleid != null) {
			sqladd.append("and id in (select rightids from ckgl_role where id="
					+ roleid + ")");
			list = DbTools.getList("select name,id from "+Right.getTable()+" where 1=1 "
					+ sqladd);
			jsonString = JSONArray.fromObject(list).toString();
			return SUCCESS;
		}
		String sql = "select name,id from "+Right.getTable()+" where modid= " + node
				+ " order by paixu";
		list = DbTools.getMapsInList(sql);
		List tmpList = new ArrayList();
		for (Map map : (ArrayList<Map>) list) {
			Tree tree = new Tree();
			tree.setCode("r"+Long.parseLong((String) map.get("id")));
			tree.setText((String) map.get("name"));
			tree.setLeaf(true);
			tree.setChecked(false);
			tmpList.add(tree);
		}
		jsonString = JSONArray.fromObject(tmpList).toString();
		//System.out.println(jsonString);
		return SUCCESS;
	}

	public Right getRight() {
		return right;
	}

	public void setRight(Right right) {
		this.right = right;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public Long getRoleid() {
		return roleid;
	}

	public void setRoleid(Long roleid) {
		this.roleid = roleid;
	}

	public Long getNode() {
		return node;
	}

	public void setNode(Long node) {
		this.node = node;
	}

}
